package com.cubeofnine.sugar.service;

import com.cubeofnine.sugar.service.element.DatabaseElement;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class DatabaseService {
	private static DatabaseHelper DB = null;

	public static final int KEY_ID = 0;
	public static final int KEY_USERNAME = 1;
	public static final int KEY_PASSWORD = 2;
	public static final int KEY_SUGAR_URL = 3;
	public static final int KEY_REMEMBER_PASSWORD = 4;
	public static final int KEY_AUTOMATIC_LOGIN = 5;
	public static final int KEY_SESSION_ID = 6;
	public static final int KEY_IS_ADMIN = 7;

	public static String getClassName() {
		return DatabaseService.class.getName();
	}

	public DatabaseService(Context context) throws Exception {
		Log.d(getClassName(), "context = " + context);
		try {
			DB = new DatabaseHelper(context);
		} catch (Exception e) {
			throw e;
		}
	}

	public void closeResults(Object... objects) throws Exception {
		Log.i(getClassName(), "closeResults() : Start");

		try {
			if (objects != null) {
				for (Object object : objects) {
					if (object instanceof Cursor) {
						((Cursor) object).close();
						object = null;
					}
				}
			}
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "closeResults() : End");
	}

	public void closeDatabase() throws Exception {
		Log.i(getClassName(), "closeDatabase() : Start");

		try {
			DB.close();
			DB = null;
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "closeDatabase() : End");
	}

	public DatabaseElement getDatabaseValue() throws Exception {
		Log.i(getClassName(), "getDatabaseValue() : Start");
		DatabaseElement result = new DatabaseElement();
		Cursor cursor = null;
		try {
			cursor = DB.select();
			Log.d(getClassName(), "cursor = " + cursor);
			if (cursor != null) {
				int rows = cursor.getCount();
				Log.d(getClassName(), "ROWs = " + rows);
				if (rows > 0) {
					if (cursor.moveToFirst()) {
						result.setId(cursor.getInt(KEY_ID));
						result.setUsername(cursor.getString(KEY_USERNAME));
						result.setPassword(cursor.getString(KEY_PASSWORD));
						result.setSugarUrl(cursor.getString(KEY_SUGAR_URL));
						result.setRememberPassword("Y".equals(cursor
								.getString(KEY_REMEMBER_PASSWORD)));
						result.setAutomaticLogin("Y".equals(cursor
								.getString(KEY_AUTOMATIC_LOGIN)));
						result.setSessionId(cursor.getString(KEY_SESSION_ID));
						result.setUserAdmin("Y".equals(cursor
								.getString(KEY_IS_ADMIN)));
					}
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			closeResults(cursor);
		}

		Log.i(getClassName(), "getDatabaseValue() : End");
		return result;
	}

	public boolean insert(String username, String password, String sugarcrmURL,
			boolean rememberPassword, boolean automaticLogin, String sessionId,
			boolean isUserAdmin) throws Exception {
		Log.i(getClassName(), "insert() : Start");
		boolean result = false;
		String rememberPasswordValue = rememberPassword ? "Y" : "N";
		String automaticLoginValue = automaticLogin ? "Y" : "N";
		String isUserAdminValue = isUserAdmin ? "Y" : "N";

		try {
			result = DB.insert(username, password, sugarcrmURL,
					rememberPasswordValue, automaticLoginValue, sessionId,
					isUserAdminValue);
		} catch (Exception e) {
			throw e;
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "insert() : End");
		return result;
	}

	public boolean updateAutomaticLogin(boolean automaticLogin)
			throws Exception {
		Log.i(getClassName(), "updateAutomaticLogin() : Start");
		boolean result = false;
		String automaticLoginValue = automaticLogin ? "Y" : "N";

		try {
			result = DB.updateAutomaticLogin(automaticLoginValue);
		} catch (Exception e) {
			throw e;
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "updateAutomaticLogin() : End");
		return result;
	}

	public boolean delete(int rowId) throws Exception {
		Log.i(getClassName(), "delete() : Start");
		boolean result = false;

		try {
			result = DB.delete(rowId);
		} catch (Exception e) {
			throw e;
		}

		Log.d(getClassName(), "result = " + result);
		Log.i(getClassName(), "delete() : End");
		return result;
	}

}
